package OSMS.action.goodsfunc;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.GoodsDao;
import OSMS.entity.Customers;
/*
 * 删除商品页面
 * 	业务逻辑如下：
 * 	1、通过request.getParameter("gid")首先从购物车页面（showCar.jsp）中获取商品编号gid并转化为int型
 * 	2、通过调用GoodsDao中的deleteGoods（cid）：来删除购物车（Car）中对应商品数据
 * 	3、选择对应的跳转方式（重定向）跳转到showCar.jsp购物车显示页面。
 */

public class carGoodsDeleteServlet extends HttpServlet{
	private GoodsDao dao = new GoodsDao();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//此处获取的gid就是detailPage.jsp中获取的全局变量：商品编码
		HttpSession session = request.getSession();
		Object obj_cus = session.getAttribute("cus");
		Customers cus = (Customers)obj_cus;
		int cid = cus.getCid();
		String gids[] = null;
		gids = request.getParameterValues("checkboxs");
		for(int i = 0;i<gids.length;i++)
			System.out.println(gids[i]);

		boolean flag = false;
		for(int i = 0;i<gids.length;i++)
		{
			try {
				flag = dao.deleteGoods(Integer.parseInt(gids[i]), cid);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		//选择对应的跳转方式（重定向）跳转到showCar.jsp购物车显示页面。
		response.sendRedirect(request.getServletContext().getContextPath() + "/jsp/user/showcar");
//		request.getRequestDispatcher("../user/showcar").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}
